import {selectListAPI,getcityAPI,cityListAPI} from "@/api/home.js"
const state = {
  selectList: [],
  location:uni.getStorageSync("location")|| {},
  city:uni.getStorageSync("city")|| {}
};
 
const mutations = {
    SET_SELECT(state, val) {
        state.selectList = val
		console.log('mutations',val)
    },
	SET_LOCTION(state, val) {
	    state.location = val
		  console.log(state)
	},
	SET_CITY(state, val) {
	    state.city = val
		console.log('city',val)
	},
};
 
const actions = {
    async fetchselectList({ state, commit }, val) {
        let data = await selectListAPI()
		commit('SET_SELECT', data)
		console.log('actions',data)
    },
	fetchlocation({ state, commit }, val) {
		 return new Promise((resolve) => {
			 if (!state.location.longitude) {
			 	  uni.getLocation({
			 		type: 'wgs84',
			 		success: function (res) {
			 			commit('SET_LOCTION', res)
						uni.setStorageSync('location',res)
						getcityAPI({
							latitude:res.latitude,
							longitude:res.longitude,
						}).then(city=>{
							uni.setStorageSync('city',city)
							commit('SET_CITY', city)
							resolve(city)
						})
			 		}
			 	})
			 } else{
				 resolve(state.location)
			 }
		})
		
		
	},
};
 
export default {
  namespaced: true,//这里一定要加true
  state,
  mutations,
  actions,
};